package exttable.analysiscreation;

import exttable.dbclasses.DataType;
import exttable.formgenerator.MessageFactory;
import exttable.formgenerator.StaticMethods;
import exttable.timetable.ETestType;
import exttable.view.ViewAnalysis;
import exttable.view.ViewAnalysisParameter;
import exttable.view.ViewSize;
import exttable.view.ViewUnits;
import utils.ELocale;
import utils.Localisation;

import javax.swing.*;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: alex
 * Date: 5/2/11
 * Time: 4:43 PM
 * To change this template use File | Settings | File Templates.
 */
public class CreateNewAnalysis implements ActionListener {
    private DefaultMutableTreeNode root;
    private int newNodeSuffix = 1;
    private DefaultTreeModel treeModel;
    private JTree tree;
    private static final String ADD_PARAMETER = "add_parameter";
    private static final String REMOVE_COMMAND = "remove";
    private static final String SAVE_COMMAND = "save";
    private static final String LOAD_OBJECT = "load";
    private JFrame frame;
    private JScrollPane scroll;
    private JButton addButton;
    private JButton removeButton;
    private JButton saveButton;
    private ViewAnalysis va;
    private List<ViewAnalysisParameter> params;
    private JPanel analysisSettings;
    private JPanel parameterSettings;
    private JPanel unitSettings;
    private JPanel minMaxSettings;
    private JPanel typeSettings;
    private JTextField nameRu;
    private JTextField nameKz;
    private JTextField nameEn;
    private JLabel lbNameRu;
    private JLabel lbNameKz;
    private JLabel lbNameEn;
    private JComboBox typeCombo;
    private JButton saveAnalysis;
    private JButton loadAnalysis;
    private SpringLayout mainLayout;

    private static List<DataType> dataTypeList;
    List<ViewAnalysisParameter> listParams = new ArrayList<ViewAnalysisParameter>();
    private int count = 0;
    private JTable table;
    private JComboBox jcbType;


    public CreateNewAnalysis() {

        root = new DefaultMutableTreeNode("Runner analysis");
        addButton = new JButton(Localisation.getMessage("create.parameter"));
        addButton.setActionCommand(ADD_PARAMETER);
        addButton.addActionListener(this);

        removeButton = new JButton(Localisation.getMessage("labelRemove"));
        removeButton.setActionCommand(REMOVE_COMMAND);
        removeButton.addActionListener(this);

        saveButton = new JButton(Localisation.getMessage("labelSaveToFile"));
        saveButton.setActionCommand(SAVE_COMMAND);
        saveButton.addActionListener(this);

        loadAnalysis = new JButton(Localisation.getMessage("labelLoadFromFile"));
        loadAnalysis.setActionCommand(LOAD_OBJECT);
        loadAnalysis.addActionListener(this);


        analysisSettings = new JPanel();

        parameterSettings = new JPanel();
        organiseParameterSettings();
        unitSettings = new JPanel();
        organiseUnitSettings();
        minMaxSettings = new JPanel();
        organiseMinMax();
        typeSettings = new JPanel();

        nameEn = new JTextField(40);
        nameKz = new JTextField(40);
        nameRu = new JTextField(40);
        lbNameEn = new JLabel("EN");
        lbNameKz = new JLabel("KZ");
        lbNameRu = new JLabel("RU");
        saveAnalysis = new JButton(Localisation.getMessage("error.save"));
        typeCombo = new JComboBox(new ETestType[]{ETestType.ANALYS, ETestType.OBJECT_DATA, ETestType.SIDE_EFFECTS});

        va = new ViewAnalysis.Build()
                .nameRu(Localisation.getMessage("create.analysisname", ELocale.RU))
                .nameEn(Localisation.getMessage("create.analysisname", ELocale.EN))
                .nameEn(Localisation.getMessage("create.analysisname", ELocale.KZ))
                .build();
        params = new ArrayList<ViewAnalysisParameter>();
        va.setParameters(params);

        tree = new JTree(new Model(va, false));
        tree.setEditable(true);
        tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
        tree.setShowsRootHandles(true);
        tree.addTreeSelectionListener(new SelectionListener());
        scroll = new JScrollPane(tree);
        scroll.setPreferredSize(new Dimension(200, 450));


        frame = new JFrame();
        analysisSettings.setPreferredSize(new Dimension(250, 75));
        frame.add(scroll);
        frame.add(addButton);
        frame.add(removeButton);
        frame.add(saveButton);
        frame.add(loadAnalysis);

        analysisSettings.setVisible(false);
        frame.add(analysisSettings);
        frame.add(parameterSettings);
        frame.add(unitSettings);
        frame.add(minMaxSettings);
        frame.add(typeSettings);
        mainLayout = new SpringLayout();
        organiseAnalysis();
        frame.setLayout(mainLayout);
        organiseLayout();
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.setSize(900, 600);

        saveAnalysis.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                va.setNameRu(nameRu.getText());
                tree.removeAll();
                tree = new JTree(new Model(va, true));
                scroll.setViewportView(tree);
                organiseLayout();
            }
        });
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        String command = e.getActionCommand();

        if (ADD_PARAMETER.equals(command)) {
            addAnalysisParameter();

        } else if (REMOVE_COMMAND.equals(command)) {
            removeCurrentNode();
        } else if (SAVE_COMMAND.equals(command)) {
            saveObject();
        } else if (LOAD_OBJECT.equals(command)) {
            loadObject();
        }
        if (listParams.size() > 0) {
            for (ViewAnalysisParameter res : listParams) {
                System.out.println(res.getNameRu());
            }
        }
    }

    private void loadObject() {
        String analysis = "c:\\person.txt";
        FileInputStream fis = null;
        ObjectInputStream in;
        ViewAnalysis val;
        try {
            fis = new FileInputStream(analysis);
            in = new ObjectInputStream(fis);
            va = (ViewAnalysis) in.readObject();
            in.close();
            if (va != null) {
                tree.removeAll();
                tree = new JTree(new Model(va, true));
                tree.addTreeSelectionListener(new SelectionListener());
                scroll.setViewportView(tree);
                organiseLayout();
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            try {
                if (fis != null) {
                    fis.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

    private void addAnalysisParameter() {

        ViewUnits vu = new ViewUnits();
        vu.setNameRu("unit");
        ViewSize vs = new ViewSize();
        ViewAnalysisParameter param = new ViewAnalysisParameter.Build()
                .nameEn(Localisation.getMessage("create.param", ELocale.EN))
                .nameKz(Localisation.getMessage("create.param", ELocale.KZ))
                .nameRu(Localisation.getMessage("create.param", ELocale.RU))
                .build();
        param.setUnit(vu);
        vs.setMaxMinValue("0-9");
        param.setSize(vs);
        DataType typeVariable = new DataType();
        typeVariable.setDataType(Localisation.getMessage("typeOfVariable"));
        param.setTypeVariable(typeVariable);
        params.add(param);
        tree.removeAll();
        tree = new JTree(new Model(va, true));
        tree.addTreeSelectionListener(new SelectionListener());
        scroll.setViewportView(tree);
        organiseLayout();
    }

    private void saveObject() {
        String analysis = "c:\\person.txt";
        FileOutputStream fos = null;
        ObjectOutputStream out = null;
        try {
            System.out.println(va.getNameRu());
            System.out.println(va.getParameters().get(0).getNameRu());

            fos = new FileOutputStream(analysis);
            out = new ObjectOutputStream(fos);
            if (va != null) {
                out.writeObject(va);
                out.close();
                System.out.println("Object Persisted");
            }

        } catch (IOException ex) {
            ex.printStackTrace();
        } finally {
            try {
                if (fos != null) {
                    fos.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if (out != null) {
                    out.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    private void removeCurrentNode() {
        TreePath currentSelection = tree.getSelectionPath();
        ViewAnalysisParameter currentNode = (ViewAnalysisParameter) currentSelection.getLastPathComponent();
        System.out.println(currentNode.getNameRu());
        params.remove(currentNode);
        tree.removeAll();
        tree = new JTree(new Model(va, true));
        tree.addTreeSelectionListener(new SelectionListener());
        scroll.setViewportView(tree);
        organiseLayout();
    }

    private void organiseAnalysis() {
        analysisSettings.setLayout(new GridLayout(4, 2));
        analysisSettings.add(lbNameEn);
        analysisSettings.add(nameEn);
        analysisSettings.add(lbNameKz);
        analysisSettings.add(nameKz);
        analysisSettings.add(lbNameRu);
        analysisSettings.add(nameRu);
        analysisSettings.add(typeCombo);
        analysisSettings.add(saveAnalysis);

    }

    public void organiseParameterSettings() {
        parameterSettings.setVisible(false);
        final JTextField nameEn = new JTextField(20);
        final JTextField nameKz = new JTextField(20);
        final JTextField nameRu = new JTextField(20);
        lbNameEn = new JLabel("EN");
        lbNameKz = new JLabel("KZ");
        lbNameRu = new JLabel("RU");
        parameterSettings.setLayout(new GridLayout(4, 2));
        parameterSettings.add(lbNameEn);
        parameterSettings.add(nameEn);
        parameterSettings.add(lbNameKz);
        parameterSettings.add(nameKz);
        parameterSettings.add(lbNameRu);
        parameterSettings.add(nameRu);
        JButton saveParameterButton = new JButton(Localisation.getMessage("error.save"));

        saveParameterButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (MessageFactory.isEmptyField(parameterSettings.getComponents())) {
                    return;
                }
                ViewAnalysisParameter vap = (ViewAnalysisParameter) tree.getLastSelectedPathComponent();
                vap.setNameRu(nameRu.getText());
                vap.setNameEn(nameEn.getText());
                vap.setNameKz(nameKz.getText());
                nameRu.setText("");
                nameEn.setText("");
                nameKz.setText("");
                tree.removeAll();
                tree = new JTree(new Model(va, true));
                tree.addTreeSelectionListener(new SelectionListener());
                scroll.setViewportView(tree);
                organiseLayout();
            }
        });
        parameterSettings.add(new JLabel(""));
        parameterSettings.add(saveParameterButton);
    }

    private void organiseUnitSettings() {
        unitSettings.setVisible(false);
        unitSettings.setLayout(new GridLayout(5, 2));
        List listUnits = StaticMethods.getUnitList();
        JLabel labelNumerator = new JLabel(Localisation.getMessage("labelSelect")
                + " " + Localisation.getMessage("labelNumerator"));
        final JComboBox jcbNumerator = new JComboBox(listUnits.toArray());
        JLabel labelDenominator = new JLabel(Localisation.getMessage("labelSelect")
                + " " + Localisation.getMessage("labelDenominator"));
        final JComboBox jcbDenominator = new JComboBox(listUnits.toArray());
        JLabel labelEnterNumerator = new JLabel(Localisation.getMessage("labelEnter")
                + " " + Localisation.getMessage("labelNumerator"));
        final JTextField jtfNumerator = new JTextField();
        JLabel labelEnterDenominator = new JLabel(Localisation.getMessage("labelEnter")
                + " " + Localisation.getMessage("labelDenominator"));
        final JTextField jtfDenominator = new JTextField();
        JLabel saveLabel = new JLabel();
        JButton saveButton = new JButton(Localisation.getMessage("error.save"));
        saveButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                ViewUnits vu = (ViewUnits) tree.getLastSelectedPathComponent();
                if (!jtfNumerator.getText().equals("") && !jtfDenominator.getText().equals("")) {
                    vu.setUnitKey(jtfNumerator.getText() + "_" + jtfDenominator.getText());
                    vu.setNameRu(vu.getUnitKey());
                } else {
                    vu.setUnitKey(jcbNumerator.getSelectedItem() + "_" + jcbDenominator.getSelectedItem());
                    vu.setNameRu(vu.getUnitKey());
                }
                tree.removeAll();
                tree = new JTree(new Model(va, true));
                tree.addTreeSelectionListener(new SelectionListener());
                scroll.setViewportView(tree);
                organiseLayout();
            }
        });
        unitSettings.add(labelNumerator);
        unitSettings.add(jcbNumerator);
        unitSettings.add(labelDenominator);
        unitSettings.add(jcbDenominator);
        unitSettings.add(labelEnterNumerator);
        unitSettings.add(jtfNumerator);
        unitSettings.add(labelEnterDenominator);
        unitSettings.add(jtfDenominator);
        unitSettings.add(saveLabel);
        unitSettings.add(saveButton);
    }

    private void organiseMinMax() {
        minMaxSettings.setVisible(false);
        minMaxSettings.setLayout(new GridLayout(3, 2));
        JLabel labelMin = new JLabel(Localisation.getMessage("labelEnter") + " " + Localisation.getMessage("labelMinValue"));
        final JTextField jtfMin = new JTextField();
        JLabel labelMax = new JLabel(Localisation.getMessage("labelEnter") + " " + Localisation.getMessage("labelMaxValue"));
        final JTextField jtfMax = new JTextField();
        JLabel saveLabel = new JLabel();
        JButton saveButton = new JButton(Localisation.getMessage("error.save"));
        saveButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                ViewSize vs = (ViewSize) tree.getLastSelectedPathComponent();
                vs.setMaxMinValue(jtfMin.getText() + "-" + jtfMax.getText());
                jtfMin.setText("");
                jtfMax.setText("");
                tree.removeAll();
                tree = new JTree(new Model(va, true));
                tree.addTreeSelectionListener(new SelectionListener());
                scroll.setViewportView(tree);
                organiseLayout();
            }
        });
        minMaxSettings.add(labelMin);
        minMaxSettings.add(jtfMin);
        minMaxSettings.add(labelMax);
        minMaxSettings.add(jtfMax);
        minMaxSettings.add(saveLabel);
        minMaxSettings.add(saveButton);
    }

    private void organiseTypeSettings() {
        final List<String> listDic = new ArrayList<String>();
        typeSettings.setVisible(false);

        SpringLayout typeSettingsLayout = new SpringLayout();
        typeSettings.setPreferredSize(new Dimension(700, 400));
        typeSettings.setLayout(typeSettingsLayout);

        final JScrollPane jScrollPaneTable = new JScrollPane();
        jScrollPaneTable.setVisible(false);
        final JButton addRowButton = new JButton(Localisation.getMessage("labelAddRow"));
        addRowButton.setVisible(false);
        final JButton removeRowButton = new JButton(Localisation.getMessage("labelRemoveRow"));
        removeRowButton.setVisible(false);
        JLabel typeLabel = new JLabel(Localisation.getMessage("labelSelect") + " " + Localisation.getMessage("typeOfVariable"));
        jcbType = new JComboBox(dataTypeList.toArray());
        final JLabel labelEnterType = new JLabel(Localisation.getMessage("labelEnterWord"));
        labelEnterType.setEnabled(false);
        final JTextField jtfEnterType = new JTextField();
        jtfEnterType.setEnabled(false);
        DataType dataType = (DataType) tree.getLastSelectedPathComponent();
        CreateAnalysisTableModel createAnalysisTableModel = new CreateAnalysisTableModel(dataType.getListDicValues(), true);
        table = new JTable(createAnalysisTableModel);

        jcbType.addItemListener(new ItemListener() {
            @Override
            public void itemStateChanged(ItemEvent e) {
                if (e.getItem().toString().equals(Localisation.getMessage("variableTypeDic"))) {
                    labelEnterType.setEnabled(true);
                    jtfEnterType.setEnabled(true);
                    jScrollPaneTable.setVisible(true);
                    addRowButton.setVisible(true);
                    removeRowButton.setVisible(true);
                } else {
                    labelEnterType.setEnabled(false);
                    jtfEnterType.setEnabled(false);
                    jScrollPaneTable.setVisible(false);
                    addRowButton.setVisible(false);
                    removeRowButton.setVisible(false);
                }
            }
        });

        JButton saveButton = new JButton(Localisation.getMessage("error.save"));
        saveButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {

                DataType dataType = (DataType) tree.getLastSelectedPathComponent();
                dataType.setDataType(((DataType) jcbType.getSelectedItem()).getDataType());
                if (((DataType) jcbType.getSelectedItem()).getDataType().equals(Localisation.getMessage("variableTypeDic"))) {
                    List<List<String>> listDicValuesTemp = new ArrayList<List<String>>();
                    List<List<String>> listDicValuesFromTable = ((CreateAnalysisTableModel) table.getModel()).getData();
                    for (List<String> list : listDicValuesFromTable) {
                        listDicValuesTemp.add(list);
                    }
                    dataType.setListDicValues(listDicValuesTemp);
                }
                tree.removeAll();
                tree = new JTree(new Model(va, true));
                tree.addTreeSelectionListener(new SelectionListener());
                scroll.setViewportView(tree);
                organiseLayout();
            }
        });

        typeSettings.add(typeLabel);
        typeSettings.add(jcbType);
        typeSettings.add(saveButton);
        jScrollPaneTable.setViewportView(table);
        typeSettings.add(jScrollPaneTable);

        addRowButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                CreateAnalysisTableModel tableModel = (CreateAnalysisTableModel) table.getModel();
                tableModel.addRow();
            }
        });
        typeSettings.add(addRowButton);

        removeRowButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                for (int index : table.getSelectedRows()) {
                    System.out.println(index);
                }
                CreateAnalysisTableModel tableModel = (CreateAnalysisTableModel) table.getModel();
                tableModel.removeRow(table.getSelectedRows());
            }
        });
        typeSettings.add(removeRowButton);


        typeSettingsLayout.putConstraint(SpringLayout.WEST, typeLabel, 0, SpringLayout.WEST, addButton);
        typeSettingsLayout.putConstraint(SpringLayout.NORTH, typeLabel, 10, SpringLayout.NORTH, addButton);
        typeSettingsLayout.putConstraint(SpringLayout.WEST, jcbType, 20, SpringLayout.EAST, typeLabel);
        typeSettingsLayout.putConstraint(SpringLayout.NORTH, jcbType, 0, SpringLayout.NORTH, typeLabel);
        typeSettingsLayout.putConstraint(SpringLayout.WEST, saveButton, 20, SpringLayout.EAST, jcbType);
        typeSettingsLayout.putConstraint(SpringLayout.NORTH, saveButton, 0, SpringLayout.NORTH, typeLabel);
        typeSettingsLayout.putConstraint(SpringLayout.WEST, jScrollPaneTable, 0, SpringLayout.WEST, typeLabel);
        typeSettingsLayout.putConstraint(SpringLayout.NORTH, jScrollPaneTable, 30, SpringLayout.NORTH, typeLabel);
        typeSettingsLayout.putConstraint(SpringLayout.WEST, addRowButton, 20, SpringLayout.EAST, saveButton);
        typeSettingsLayout.putConstraint(SpringLayout.NORTH, addRowButton, 0, SpringLayout.NORTH, typeLabel);
        typeSettingsLayout.putConstraint(SpringLayout.WEST, removeRowButton, 0, SpringLayout.WEST, addRowButton);
        typeSettingsLayout.putConstraint(SpringLayout.NORTH, removeRowButton, 10, SpringLayout.SOUTH, addRowButton);
    }

    private void organiseLayout() {
        mainLayout.putConstraint(SpringLayout.WEST, analysisSettings, 5, SpringLayout.EAST, scroll);
        mainLayout.putConstraint(SpringLayout.NORTH, analysisSettings, 5, SpringLayout.SOUTH, saveButton);

        mainLayout.putConstraint(SpringLayout.WEST, scroll, 5, SpringLayout.WEST, frame);
        mainLayout.putConstraint(SpringLayout.NORTH, scroll, 1, SpringLayout.SOUTH, frame);

        mainLayout.putConstraint(SpringLayout.WEST, addButton, 5, SpringLayout.EAST, scroll);
        mainLayout.putConstraint(SpringLayout.NORTH, addButton, 1, SpringLayout.SOUTH, frame);

        mainLayout.putConstraint(SpringLayout.WEST, removeButton, 5, SpringLayout.EAST, addButton);
        mainLayout.putConstraint(SpringLayout.NORTH, removeButton, 1, SpringLayout.SOUTH, frame);

        mainLayout.putConstraint(SpringLayout.WEST, saveButton, 5, SpringLayout.EAST, scroll);
        mainLayout.putConstraint(SpringLayout.NORTH, saveButton, 30, SpringLayout.SOUTH, frame);

        mainLayout.putConstraint(SpringLayout.WEST, loadAnalysis, 5, SpringLayout.EAST, saveButton);
        mainLayout.putConstraint(SpringLayout.NORTH, loadAnalysis, 30, SpringLayout.SOUTH, frame);

        mainLayout.putConstraint(SpringLayout.WEST, parameterSettings, 5, SpringLayout.EAST, scroll);
        mainLayout.putConstraint(SpringLayout.NORTH, parameterSettings, 5, SpringLayout.SOUTH, saveButton);

        mainLayout.putConstraint(SpringLayout.WEST, unitSettings, 5, SpringLayout.EAST, scroll);
        mainLayout.putConstraint(SpringLayout.NORTH, unitSettings, 5, SpringLayout.SOUTH, saveButton);

        mainLayout.putConstraint(SpringLayout.WEST, minMaxSettings, 5, SpringLayout.EAST, scroll);
        mainLayout.putConstraint(SpringLayout.NORTH, minMaxSettings, 5, SpringLayout.SOUTH, saveButton);

        mainLayout.putConstraint(SpringLayout.WEST, typeSettings, 5, SpringLayout.EAST, scroll);
        mainLayout.putConstraint(SpringLayout.NORTH, typeSettings, 5, SpringLayout.SOUTH, saveButton);
    }


    class SelectionListener implements TreeSelectionListener {

        @Override
        public void valueChanged(TreeSelectionEvent e) {
            JTree tree = (JTree) e.getSource();


            if (tree.getLastSelectedPathComponent() instanceof ViewAnalysis) {
                ViewAnalysis currentNode = (ViewAnalysis) tree.getLastSelectedPathComponent();
                System.out.println(currentNode.getNameRu());
                analysisSettings.setVisible(true);
                parameterSettings.setVisible(false);
                unitSettings.setVisible(false);
                minMaxSettings.setVisible(false);
                typeSettings.setVisible(false);
            }

            if (tree.getLastSelectedPathComponent() instanceof ViewAnalysisParameter) {
                ViewAnalysisParameter currentNode = (ViewAnalysisParameter) tree.getLastSelectedPathComponent();
                analysisSettings.setVisible(false);
                parameterSettings.setVisible(true);
                unitSettings.setVisible(false);
                minMaxSettings.setVisible(false);
                typeSettings.setVisible(false);
            }

            if (tree.getLastSelectedPathComponent() instanceof ViewUnits) {
                ViewUnits currentNode = (ViewUnits) tree.getLastSelectedPathComponent();
                analysisSettings.setVisible(false);
                parameterSettings.setVisible(false);
                unitSettings.setVisible(true);
                minMaxSettings.setVisible(false);
                typeSettings.setVisible(false);
            }
            if (tree.getLastSelectedPathComponent() instanceof ViewSize) {
                ViewSize currentNode = (ViewSize) tree.getLastSelectedPathComponent();
                analysisSettings.setVisible(false);
                parameterSettings.setVisible(false);
                unitSettings.setVisible(false);
                minMaxSettings.setVisible(true);
                typeSettings.setVisible(false);
            }
            if (tree.getLastSelectedPathComponent() instanceof DataType) {
                DataType dataType = (DataType) tree.getLastSelectedPathComponent();
                analysisSettings.setVisible(false);
                parameterSettings.setVisible(false);
                unitSettings.setVisible(false);
                minMaxSettings.setVisible(false);
                if (count == 0) {
                    organiseTypeSettings();
                    count++;
                } else {

                    table.setModel(new CreateAnalysisTableModel(dataType.getListDicValues(), true));
                }
                if (!dataType.getDataType().equals(Localisation.getMessage("typeOfVariable"))) {
                    setIndexInJComboBox(jcbType, dataType.getDataType());
                } else {
                    jcbType.setSelectedIndex(0);
                }
                typeSettings.setVisible(true);
                /*
                for (List<String> stringList : dataType.getListDicValues()) {
                    for (String str : stringList) {
                        System.out.println("value : " + str);
                    }

                }*/

            }


        }
    }

    static {
        dataTypeList = new ArrayList<DataType>();
        DataType variableTypeInt = new DataType();
        variableTypeInt.setDataType(Localisation.getMessage("variableTypeInt"));

        DataType variableTypeDouble = new DataType();
        variableTypeDouble.setDataType(Localisation.getMessage("variableTypeDouble"));

        DataType variableTypeBoolean = new DataType();
        variableTypeBoolean.setDataType(Localisation.getMessage("variableTypeBoolean"));

        DataType variableTypeDic = new DataType();
        variableTypeDic.setDataType(Localisation.getMessage("variableTypeDic"));

        dataTypeList.add(variableTypeInt);
        dataTypeList.add(variableTypeDouble);
        dataTypeList.add(variableTypeBoolean);
        dataTypeList.add(variableTypeDic);

    }

    public static void setIndexInJComboBox(JComboBox jComboBox, Object object) {
        for (int i = 0; i < jComboBox.getItemCount(); i++) {
            if (jComboBox.getItemAt(i).toString().equals(object.toString())) {
                jComboBox.setSelectedItem(jComboBox.getItemAt(i));
            }
        }
    }
}
